<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>数据备份</title>
	<!--[if IE]>
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<![endif]-->
	<!-- Main Stylesheet --> 
	<link rel="stylesheet" href="<%=request.getContextPath()%>/css/style.css" type="text/css" />
	<!-- Your Custom Stylesheet --> 
	<link rel="stylesheet" href="<%=request.getContextPath()%>/css/custom.css" type="text/css" />
	<link href="<%=request.getContextPath()%>/lib/thickbox/thickbox.css" rel="stylesheet" type="text/css" />
	<link href="<%=request.getContextPath()%>/lib/jquery/jquery.autocomplete.css" type="text/css" rel="stylesheet"/>
	<style>
	<!--
		#column_list input{
			margin-right:10px;
		}
	-->
	</style>
	<%@include file="/include/header.jsp" %>
	<script src="<%=request.getContextPath()%>/lib/jquery/jquery-1.4.2.min.js"></script>
	<script src="<%=request.getContextPath()%>/lib/validator/formValidator_min.js"></script>
	<script src="<%=request.getContextPath()%>/lib/validator/formValidatorRegex.js"></script>
	<script src="<%=request.getContextPath()%>/lib/jquery/jquery.form.js"></script>
	<script src="<%=request.getContextPath()%>/lib/datepicker/WdatePicker.js"></script>
	<script src="<%=request.getContextPath()%>/lib/thickbox/thickbox.js"></script>
	<script src="<%=request.getContextPath()%>/lib/jquery/jquery.autocomplete.pack.js"></script>
	<script src="<%=request.getContextPath()%>/js/function.js"></script>
	<script type="text/javascript">
		var path = "<%=request.getContextPath()%>";
		$(document).ready(function(){
			//增加数据表
			$.formValidator.initConfig({
	  			validatorGroup: "1",
				formID:"addTableForm",
				onError:function(msg){
					showTip(msg);
				},
				onSuccess:function(){
					addRow(getById('etcTable'), {
						"tableName": $('#tableName').val(),
						"tableText": $('#tableText').val()
					});
					tb_remove();
					return false;
				}
			});
			$("#tableName").formValidator({
	  			validatorGroup: "1",
				onShow:"请输入数据库表名",
				onFocus:"2-32个字符"
			}).inputValidator({min:2,max:32});
			//自定义数据表
			$.formValidator.initConfig({
	  			validatorGroup: "3",
				formID:"addCustomTableForm",
				onError:function(msg){
					showTip(msg);
				},
				onSuccess:function(){
					addCustomRow(getById('customTable'), {
						"sql": $('#sql').val(),
						"customTableName": $('#customTableName').val()
					});
					tb_remove();
					return false;
				}
			});
			$("#sql").formValidator({
	  			validatorGroup: "3",
				onShow:"SQL查询语句，应返回结果集"
			}).inputValidator({min:10});
			$("#customTableName").formValidator({
	  			validatorGroup: "3",
				onShow:"请输入数据库表名",
				onFocus:"2-32个字符"
			}).inputValidator({min:2,max:32});
			//数据备份文件上传
			$.formValidator.initConfig({
	  			validatorGroup: "2",
				formID:"renewForm",
				onError:function(msg){
					showTip(msg);
				},
				onSuccess:function(){
					if( confirm("准备提交数据，是否继续？") ){
						Mask.show();
						$('#renewForm').ajaxSubmit({
							dataType: 'json',
							success: function(json){
								Ajax.callback(json);
								tb_remove();
							}
						});
					}
					return false;
				}
			});
			$("#filename").formValidator({
	  			validatorGroup: "2",
				onShow:"备份文件为扩展名backup"
			}).inputValidator({min:1});
		});
		function refresh(){
			document.location.reload();
		}
		function addTable(){
			getById("addTableForm").reset();
			showBox("#TB_inline?height=120&width=500&inlineId=add_area", "增加数据表");
		}
		function addCustomTable(){
			getById("addCustomTableForm").reset();
			showBox("#TB_inline?height=120&width=550&inlineId=customTable_area", "自定义数据");
		}
		//显示数据表的列
		function showColumn(tableName){
			$('#hidden_table_name').val(tableName);
			Mask.show();
			jQuery.ajax({
	  			url:path+'/protected/system/showColumnAdvancedConfig.action',
	  			dataType:'json',
	  			type:'POST',
	  			data:{"tableName" : tableName},
	  			success:function(json){
	  				Mask.hide();
	  				var html = [];
	  				if(!json.success && json.data){
	  					Alert(json.data);
	  					return;
	  				}
	  				if(json.data){
	  					html.push("<table class='no-style'><tr>");
	  					for(var i=0; i<json.data.length; i++){
	  						html.push("<td><input type='checkbox' name='tableColumnName' value='"+json.data[i]+"' checked/>"+json.data[i]+"</td>");
	  						if((i+1)%3 == 0){
	  							html.push("</tr><tr>");
	  						}
	  					}
	  					html.push("</tr></table>");
	  				}
	  				$("#column_list").html(html.join(''));
	  				showBox("#TB_inline?height=300&width=450&inlineId=column_area", "请选择列");
	  			},error:Ajax.callback
	  		});
		}
		
		function chooseColumn(){
			var columns = getChecked('tableColumnName');
			var tableName = $('#hidden_table_name').val();
			if(columns.length > 0){
				$("#td_"+tableName).html(columns.join(", "));
			}else{
				$("#td_"+tableName).html("全部");
			}
			tb_remove();
		}
		
		function addRow(table, data){
			var row = document.createElement("TR");
			
			var tableNameCell = document.createElement("TD");
			tableNameCell.className = "td2";
			tableNameCell.innerHTML = '<input type="checkbox" name="etcTableName" value="'+data['tableName']+'" />';
			
			var tableTextCell = document.createElement("TD");
			tableTextCell.className = "td2";
			tableTextCell.innerHTML = data['tableText'];
			
			var columnCell = document.createElement("TD");
			SetAttribute(columnCell, 'id', "td_"+data['tableName']);
			columnCell.className = "td2";
			columnCell.innerHTML = '全部';
			
			var opeCell = document.createElement("TD");
			opeCell.className = "td2";
			var opeHTML = '<a href="javascript:void(0);" onclick="showColumn(\''+data['tableName']+'\');">［请选择］</a> ';
			opeHTML += '<a href="javascript:void(0);" onclick="javascript:removeRow(this);">［删除］</a>';
			opeCell.innerHTML = opeHTML;

			row.appendChild(tableNameCell);
			row.appendChild(tableTextCell);
			row.appendChild(columnCell);
			row.appendChild(opeCell);
			var bodies = table.tBodies;
			if(bodies){
				bodies[0].appendChild(row);
			}else{
				table.appendChild(row);
			}
		}
		function addCustomRow(table, data){
			var row = document.createElement("TR");
			
			var inputCell = document.createElement("TD");
			inputCell.className = "td2";
			inputCell.innerHTML = '<input type="checkbox" name="customTableName" value="'+data['customTableName']+'" />';
			
			var sqlCell = document.createElement("TD");
			SetAttribute(sqlCell, 'id', "custom_"+data['customTableName']);
			sqlCell.className = "td2";
			sqlCell.innerHTML = data['sql'];
			
			var tableNameCell = document.createElement("TD");
			tableNameCell.className = "td2";
			tableNameCell.innerHTML = data['customTableName'];
			
			var opeCell = document.createElement("TD");
			opeCell.className = "td2";
			var opeHTML = '<a href="javascript:void(0);" onclick="javascript:removeRow(this,\'customTable\');">［删除］</a>';
			opeCell.innerHTML = opeHTML;

			row.appendChild(inputCell);
			row.appendChild(sqlCell);
			row.appendChild(tableNameCell);
			row.appendChild(opeCell);
			var bodies = table.tBodies;
			if(bodies){
				bodies[0].appendChild(row);
			}else{
				table.appendChild(row);
			}
		}
		function removeRow(delEl, tablename){
			tablename = tablename || 'etcTable';
			var table = document.getElementById(tablename);
			var row = delEl.parentNode.parentNode;
			var bodies = table.tBodies;
			if(bodies){
				bodies[0].removeChild(row);
			}else{
				table.removeChild(row);
			}
		}
		//开始分析数据并备份
		function backup(){
			var selects  = [];
			var tables = [];
			//系统基础数据表
			var baseTableName = getChecked('baseTableName');
			for(var i=0; i<baseTableName.length; i++){
				selects.push('select * from '+baseTableName[i]);
				tables.push(baseTableName[i]);
			}
			//其他数据表
			var es = document.getElementsByName('etcTableName');
			for(var i=0; i<es.length; i++){
				if(es[i].checked){
					var td = es[i].parentNode.parentNode;
					var column = $(td).children("td")[2].innerHTML;
					if(column != '全部'){
						selects.push("select "+$(td).children("td")[2].innerHTML+" from "+es[i].value);
					}else{
						selects.push('select * from '+es[i].value);
					}
					tables.push(es[i].value);
				}
			}
			//自定义数据表
			var es = document.getElementsByName('customTableName');
			for(var i=0; i<es.length; i++){
				if(es[i].checked){
					var td = es[i].parentNode.parentNode;
					var sql = $(td).children("td")[1].innerHTML;
					selects.push(sql);
					tables.push(es[i].value);
				}
			}
			//Alert(selects.join('\r\n'));Alert(tables.join('\r\n'));
			if(selects.length < 1 || tables.length < 1){ showTip('请选择数据表');return;}
			var encoding = getChecked("encoding"); //得到文件编码
			if( confirm("是否立即开始备份？") ){
				openPostWindow(path+'/protected/system/backupAdvancedConfig.action',
					{"selects" : selects.join(";"), "tables":tables.join(";"), "encoding": encoding},
					"_blank"
				);
			}
		}
		//设置文件编码
		function setEncoding(){
			showBox("#TB_inline?height=60&width=520&inlineId=encoding_area", "设置文件编码");
		}
		//从备份文件恢复数据
		function renew(){
			getById("renewForm").reset();
			showBox("#TB_inline?height=140&width=560&inlineId=renew_area", "数据恢复");
		}
		function onFileChange(file){
			var check = function(filename){
				var start = filename.lastIndexOf(".");
				if(start == -1){
					return false;
				}
				var suffix = filename.substring(start+1);
				return suffix == 'backup';
			}
			if(! check(file.value)){
				showTip('文件格式不对，只支持本系统导出的备份文件，以.backup为扩展名');
				document.getElementById("renewForm").reset();
			}
		}
	</script>
</head>
<body>
	<%@include file="/include/body.jsp" %>
	
<s:if test="flag">
	<!-- 显示操作成功 -->
	<script type="text/javascript">
		showTip("操作成功");
	</script>
</s:if>

<!-- Wrapper -->
<div class="wrapper">
    
    <div class="content-box">
	<header>
		<h3>
			<span>数据表</span>
		</h3>
		<div>
			<span>
	    		<span>
	    			<a class="btn" href="javascript:void(0)" onclick="refresh();"><span class="icon icon-refresh"></span>刷新</a>
	    		</span>
	    		<span>
	    			<a class="btn" href="javascript:void(0)" onclick="setEncoding();">设置编码..</a>
	    		</span>
	    		<span>
	    			<a class="btn" href="javascript:void(0)" onclick="backup();">开始备份</a>
	    		</span>
	    		<span>
	    			<a class="btn" href="javascript:void(0)" onclick="renew();">数据恢复..</a>
	    		</span>
	    	</span>
		</div>
	</header>
	<section>
		<table style="width:100%;">
		 	<tr><td style="text-align:center;vertical-align:top;" width="15%">
		 		<div class="content-box">
				<header>
				    <h3>
						<span>数据表</span>
					</h3>
				</header>
				<section>
				    <table class="stylized" style="width:95%;">
						<tr>
							<th><input type="checkbox" onclick="checkAll(this,'baseTableName')" checked/></th><th>系统数据表</th>
						</tr>
						<s:iterator status="stat" value="#{'sec_user':'用户表','sec_auth':'权限表','sec_menu':'菜单表','sec_role':'角色表','sec_group':'用户组表','sec_user_role':'用户角色关联表','sec_user_auth':'用户权限关联表','sec_role_auth':'角色权限关联表','sec_url_auth':'URL权限关联表','sec_menu_auth':'菜单权限关联表'}">
						<tr>
							<td><input type="checkbox" name="baseTableName" value="<s:property value="key"/>" checked/></td>
							<td><s:property value="value"/></td>
						</tr>
						</s:iterator>
					</table>
				</section>
				</div>
		 		</td>
		 		<td style="text-align:center;vertical-align:top;" width="45%">
		 		<div class="content-box">
				<header>
				    <h3>
						<span>其他表</span>
					</h3>
					<div>
						<span>
				    		<span>
				    			<a class="btn" href="javascript:void(0)" onclick="addTable();" title="增加要备份的数据表"><span class="icon icon-add"></span>增加..</a>
				    		</span>
				    	</span>
					</div>
				</header>
				<section>
				    <table id="etcTable" class="stylized" style="width:98%;">
						<tr>
							<th><input type="checkbox" onclick="checkAll(this,'etcTableName')"/></th>
							<th>其他数据表</th>
							<th width="150">数据列</th>
							<th>选择列</th>
						</tr>
						<tr>
							<td><input type="checkbox" name="etcTableName" value="sec_actionlog" /></td>
							<td>操作日志表</td>
							<td id="td_t_sec_actionlog">全部</td>
							<td>
								<a href="javascript:void(0);" onclick="showColumn('sec_actionlog');">［请选择］</a>
								<a href="javascript:void(0);" onclick="javascript:removeRow(this);">［删除］</a>
							</td>
						</tr>
						<tr>
							<td><input type="checkbox" name="etcTableName" value="sec_errorlog" /></td>
							<td>错误日志表</td>
							<td id="td_t_sec_errorlog">全部</td>
							<td>
								<a href="javascript:void(0);" onclick="showColumn('sec_errorlog');">［请选择］</a>
								<a href="javascript:void(0);" onclick="javascript:removeRow(this);">［删除］</a>
							</td>
						</tr>
					</table>
				</section>
				</div>
		 		</td>
		 		<td style="text-align:center;vertical-align:top;" width="40%">
		 		<div class="content-box">
				<header>
				    <h3>
						<span>自定义</span>
					</h3>
					<div>
						<span>
				    		<span>
				    			<a class="btn" href="javascript:void(0)" onclick="addCustomTable();" title="增加要备份的数据表"><span class="icon icon-add"></span>增加..</a>
				    		</span>
				    	</span>
					</div>
				</header>
				<section>
				    <table class="stylized" id="customTable" style="width:98%;">
						<tr>
							<th><input type="checkbox" onclick="checkAll(this,'customTableName')"/></th>
							<th>SQL</th>
							<th>表名</th>
							<th>操作</th>
						</tr>
					</table>
				</section>
				</div>
		 		</td>
		 	</tr>
		 </table>
	</section>
	</div>
</div>

<div id="add_area" style="display:none;">
	 <div id="add_area_wrap">
		<form id="addTableForm" action="" method="post">
		<table class="no-style">
			<tr>
				<td><label class="required">数据库表名</label></td>
				<td><input id="tableName" type="text" name="tableName" value=""/></td>
				<td><div id="tableNameTip"></div></td>
			</tr>
			<tr>
				<td><label>表显示名</label></td>
				<td><input id="tableText" type="text" name="tableText" value=""/></td>
				<td><div id="tableTextTip"></div></td>
			</tr>
		</table>
		<div class="form-btn">
			<button class="btn btn-green" type="submit"><span class="icon icon-ok"></span>提 交</button>
			<button class="btn" type="reset" onclick="javascript:tb_remove();"><span class="icon icon-close"></span>取消</button>
		</div>
		</form>
	</div>
</div>
<div id="customTable_area" style="display:none;">
	 <div id="add_area_wrap">
		<form id="addCustomTableForm" action="" method="post">
		<table class="no-style">
			<tr>
				<td><label class="required">SQL查询语句</label></td>
				<td><input id="sql" type="text" name="sql" value=""/></td>
				<td><div id="sqlTip"></div></td>
			</tr>
			<tr>
				<td><label class="required">表名</label></td>
				<td><input id="customTableName" type="text" name="customTableName" value=""/></td>
				<td><div id="customTableNameTip"></div></td>
			</tr>
		</table>
		<div class="form-btn">
			<button class="btn btn-green" type="submit"><span class="icon icon-ok"></span>提 交</button>
			<button class="btn" type="reset" onclick="javascript:tb_remove();"><span class="icon icon-close"></span>取消</button>
		</div>
		</form>
	</div>
</div>
<div id="column_area" style="display:none;">
	 <div id="column_area_wrap" style="height:100%;width:100%;">
	 	<input type="hidden" id="hidden_table_name" value=""/>
		<div id="column_list" style="overflow:auto;height:85%;">
			
		</div>
		<div>
			<span style="margin-right:2px;"><input type="checkbox" onclick="checkAll(this, 'tableColumnName');"/></span>
			<span style="margin-right:10px;">全选</span>
			<button class="btn btn-green" type="button" onclick="chooseColumn();"><span class="icon icon-ok"></span>确定</button>
		</div>
	</div>
</div>
<div id="renew_area" style="display:none;">
	 <div id="renew_area_wrap" style="height:100%;width:100%;">
		<form id="renewForm" action="<%=request.getContextPath()%>/protected/system/renewAdvancedConfig.action" method="post" enctype="multipart/form-data">
		<div id="encoding_area" style="display:none;">
			 <div id="encoding_area_wrap" style="height:100%;width:100%;">
				<table class="no-style">
					<tr>
						<td>文件编码</td>
						<td>
							<input class="mlr5" type="radio" name="encoding" value="UTF-8" checked/>UTF-8
							<input class="mlr5" type="radio" name="encoding" value="GBK"/>GBK
							<input class="mlr5" type="radio" name="encoding" value="GB_2312"/>GB_2312
							<input class="mlr5" type="radio" name="encoding" value="UTF-16"/>UTF-16
							<input class="mlr5" type="radio" name="encoding" value="ISO-8859-1"/>ISO-8859-1
						</td>
					</tr>
				</table>
				<div>
					<button class="btn btn-green" type="button" onclick="tb_remove();"><span class="icon icon-ok"></span>确定</button>
					<button class="btn" type="reset" onclick="javascript:tb_remove();"><span class="icon icon-close"></span>取消</button>
				</div>
			</div>
		</div>
		<table class="no-style">
			<tr>
				<td><label class="required">上传文件名</label></td>
				<td><input id="filename" onchange="onFileChange(this);" type="file" name="filename" value=""/></td>
				<td><div id="filenameTip"></div></td>
			</tr>
			<tr>
				<td><label>恢复数据前清空表</label></td>
				<td><input name="clearBeforeInsert" type="checkbox" value="true" checked/></td>
				<td></td>
			</tr>
			<tr>
				<td><label>遇到错误时跳过继续执行</label></td>
				<td><input name="skipError" type="checkbox" value="true"/></td>
				<td></td>
			</tr>
		</table>
		<div class="form-btn">
			<button class="btn btn-green" type="submit"><span class="icon icon-ok"></span>上传</button>
			<button class="btn" type="reset" onclick="javascript:tb_remove();"><span class="icon icon-close"></span>取消</button>
		</div>
		</form>
	</div>
</div>
</body>
</html>